package gui;

import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.AbstractButton;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.KeyStroke;
import javax.swing.border.EmptyBorder;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;

import core.Usuario;

public class Login extends JDialog {

    private static final long serialVersionUID = 78928912605018205L;
    public JTextField campoUsuario;
    public JPasswordField campoSenha;
    private AbstractButton botaoLogin;
    private AbstractButton botaoDeletar;
    private JButton botaoNovo;

    public Login(GUI parente) {
	super(parente, "Login", true);

	addWindowListener(new WindowAdapter() {

	    public void windowClosing(WindowEvent ev) {
		System.exit(0);
	    }
	});

	int width = 270;
	int height = 120;

	setBounds(parente.getX() + ((parente.getWidth() - width) / 2), parente.getY() + ((parente.getHeight() - height) / 2), width, height);

	JPanel contentPane = new JPanel();
	contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
	setContentPane(contentPane);

	contentPane.setLayout(null);

	// Adicionar componentes
	int rotulosX = 5;
	int camposX = 54;

	int x = 0;
	int y = 8;

	JLabel rotuloUsuario = new JLabel("Usuario:");
	rotuloUsuario.setBounds(rotulosX, y, 40, 20);
	contentPane.add(rotuloUsuario);

	campoUsuario = new JTextField();
	campoUsuario.registerKeyboardAction(parente, "login_ok", KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), JComponent.WHEN_FOCUSED);
	campoUsuario.setBounds(camposX, y, 203, 20);
	contentPane.add(campoUsuario);

	y += 30;

	JLabel rotuloSenha = new JLabel("Senha:");
	rotuloSenha.setBounds(rotulosX, y, 40, 20);
	contentPane.add(rotuloSenha);

	campoSenha = new JPasswordField();
	campoSenha.registerKeyboardAction(parente, "login_ok", KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), JComponent.WHEN_FOCUSED);
	campoSenha.setBounds(camposX, y, 203, 20);
	contentPane.add(campoSenha);

	x = rotulosX;
	y += 25;

	botaoLogin = new JButton("Login");
	botaoLogin.setFocusPainted(true);
	botaoLogin.setActionCommand("login_ok");
	botaoLogin.addActionListener(parente);
	botaoLogin.setBounds(x, y, 60, 20);
	contentPane.add(botaoLogin);

	x += 60;

	botaoDeletar = new JButton("Deletar");
	botaoDeletar.setActionCommand("delete_user");
	botaoDeletar.addActionListener(parente);
	botaoDeletar.setBounds(x, y, 68, 20);
	contentPane.add(botaoDeletar);

	x += 68;

	botaoNovo = new JButton("Novo");
	botaoNovo.setActionCommand("cadastrar");
	botaoNovo.addActionListener(parente);
	botaoNovo.setBounds(x, y, 60, 20);
	contentPane.add(botaoNovo);

	x += 60;

	JButton botaoFechar = new JButton("Fechar");
	botaoFechar.setActionCommand("exit");
	botaoFechar.addActionListener(parente);
	botaoFechar.setBounds(x, y, 65, 20);
	contentPane.add(botaoFechar);

	CaretListener camposListener = new CaretListener() {
	    @Override
	    public void caretUpdate(CaretEvent e) {
		updateButons();
	    }
	};

	campoUsuario.addCaretListener(camposListener);
	campoSenha.addCaretListener(camposListener);

	setResizable(false);

	updateButons();
    }

    public Usuario getUsuario() {
	return new Usuario(campoUsuario.getText(), campoSenha.getPassword());
    }

    public void clear() {
	campoUsuario.setText(null);
	campoSenha.setText(null);
    }

    public void updateButons() {
	boolean hasText = !campoUsuario.getText().isEmpty() && campoSenha.getPassword().length > 0;
	boolean hasUser = GUI.udb.hasUser(getUsuario());
	boolean hasUserName = GUI.udb.hasUserName(getUsuario());
	botaoLogin.setEnabled(hasText && hasUser);
	botaoNovo.setEnabled(hasText && !hasUserName);
	botaoDeletar.setEnabled(hasText && hasUser);
    }
}
